package com.dawn.trademanage.serviceimpl;

import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.StaleObjectStateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dawn.trademanage.bean.TradeDetailBean;
import com.dawn.trademanage.bean.TradeHistoryBean;
import com.dawn.trademanage.bean.TradeInfoBean;
import com.dawn.trademanage.dao.TradeManageDao;
import com.dawn.trademanage.service.TradeManageService;
import com.dawn.utils.CalculateTime;
import com.dawn.utils.exceptions.DaoAccessException;

@Service
public class TradeManageServiceImpl implements TradeManageService {

	private static Logger logger = Logger
			.getLogger(TradeManageServiceImpl.class);

	@Autowired
	private TradeManageDao tradeManageDao;

	public boolean addTrade(TradeInfoBean tradeInfo) {
		try {
			tradeManageDao.addTrade(tradeInfo);
		} catch (Exception e) {
			logger.error("Data access error.");
			e.printStackTrace();
			throw new DaoAccessException();
		}
		return true;
	}

	public List<TradeInfoBean> showTradeList() {
		List<TradeInfoBean> list = null;
		try {
			list = tradeManageDao.showTradeList();
		} catch (Exception e) {
			logger.error("Data access error.");
			e.printStackTrace();
			throw new DaoAccessException();
		}
		return list;
	}

	public TradeInfoBean findTradeById(long tradeId) {
		try {
			return tradeManageDao.findTradeById(tradeId);
		} catch (Exception e) {
			logger.error("Data access error.");
			e.printStackTrace();
			throw new DaoAccessException();
		}
	}

	public void updateTrade(TradeInfoBean tradeInfoBean) {
		try {
			tradeManageDao.updateTradeInfo(tradeInfoBean);
		} catch (Exception e) {
			logger.error("Data access error.");
			e.printStackTrace();
			throw new DaoAccessException();
		}
	}

	public boolean findIsFollowed(long userId, long tradeId) {
		try {
			return tradeManageDao.findIsFollowed(userId, tradeId);
		} catch (Exception e) {
			logger.error("Data access error.");
			e.printStackTrace();
			throw new DaoAccessException();
		}
	}

	public boolean addTradeDetail(TradeDetailBean tradeDetailBean) {
		try {
			tradeManageDao.addTradeDetail(tradeDetailBean);
			return true;
		} catch (Exception e) {
			logger.error("Data access error.");
			e.printStackTrace();
			throw new DaoAccessException();
		}
	}

	public boolean increaseBalance(long tradeId, int num) {
		try {
			TradeInfoBean tradeInfo = tradeManageDao.findTradeById(tradeId);
			if (tradeInfo == null)
				return false;
			tradeInfo.setBalance(tradeInfo.getBalance() + num);
			tradeManageDao.updateTradeInfo(tradeInfo);
			return true;
		} catch (Exception e) {
			logger.error("Data access error.");
			e.printStackTrace();
			throw new DaoAccessException();
		}
	}

	public boolean reduceBalance(long tradeId, int num) {
		try {
			TradeInfoBean tradeInfo = tradeManageDao.findTradeById(tradeId);
			if (tradeInfo == null)
				return false;
			int remaining = (int) (tradeInfo.getBalance() - num);
			if (remaining <= 0) {
				stopTrade(tradeId);
				return false;
			}
			if (tradeInfo.getIsStop() != 0)
				return false;
			tradeInfo.setBalance(remaining);
			tradeManageDao.updateTradeInfo(tradeInfo);
			return true;
		} catch (Exception e) {
			logger.error("Data access error.");
			e.printStackTrace();
			throw new DaoAccessException();
		}
	}

	public void beginTrade(long tradeId) {
		try {
			TradeInfoBean tradeInfo = tradeManageDao.findTradeById(tradeId);
			if (tradeInfo.getIsStop() == 0)
				return;
			tradeInfo.setIsStop((byte) 0);
			tradeInfo.setLastBeginTime(Calendar.getInstance());
			tradeManageDao.updateTradeInfo(tradeInfo);
		} catch (Exception e) {
			logger.error("Data access error.");
			e.printStackTrace();
			throw new DaoAccessException();
		}
	}

	public void stopTrade(long tradeId) {
		try {
			TradeInfoBean tradeInfo = tradeManageDao.findTradeById(tradeId);
			if (tradeInfo.getIsStop() == 1)
				return;
			tradeInfo.setIsStop((byte) 1);
			TradeHistoryBean tradeHistory = new TradeHistoryBean();
			Calendar startTime = tradeInfo.getLastBeginTime();
			Calendar endTime = Calendar.getInstance();
			tradeHistory.setBeginTime(startTime);
			tradeHistory.setOverTime(endTime);
			tradeHistory.setTtTradeinfo(tradeInfo);
			List<TradeDetailBean> list = tradeManageDao.listFollowers(tradeId,
					CalculateTime.getDateStringForSql(startTime),
					CalculateTime.getDateStringForSql(endTime));
			tradeHistory.setGrowNum(list.size());
			tradeManageDao.updateTradeInfo(tradeInfo);
			tradeManageDao.addTradeHistory(tradeHistory);
		} catch (Exception e) {
			logger.error("Data access error.");
			e.printStackTrace();
			throw new DaoAccessException();
		}
	}

	public List<TradeDetailBean> listFollowers(Long tradeId) {
		List<TradeDetailBean> list = null;
		try {
			Calendar startTime = Calendar.getInstance();
			startTime.clear();
			Calendar endTime = Calendar.getInstance();
			String startTimeStr = CalculateTime.getDateStringForSql(startTime);
			String endTimeStr = CalculateTime.getDateStringForSql(endTime);
			list = (List<TradeDetailBean>) tradeManageDao.listFollowers(
					tradeId, startTimeStr, endTimeStr);
		} catch (Exception e) {
			logger.error("Data access error.");
			e.printStackTrace();
			throw new DaoAccessException();
		}
		return list;
	}

	public List<TradeDetailBean> listIdols(Long userId) {
		List<TradeDetailBean> list = null;
		try {
			Calendar startTime = Calendar.getInstance();
			startTime.clear();
			Calendar endTime = Calendar.getInstance();
			String startTimeStr = CalculateTime.getDateStringForSql(startTime);
			String endTimeStr = CalculateTime.getDateStringForSql(endTime);
			list = (List<TradeDetailBean>) tradeManageDao.listIdols(userId,
					startTimeStr, endTimeStr);
		} catch (Exception e) {
			logger.error("Data access error.");
			e.printStackTrace();
			throw new DaoAccessException();
		}
		return list;
	}
}
